//
//  SDGetInfoViewController.swift
//  Seed
//
//  Created by Samuel on 2017/6/5.
//  Copyright © 2017年 RedSoft. All rights reserved.
//  手机注册完善个人信息（或修改个人信息）

import UIKit
import SVProgressHUD
import ImagePicker

class SDGetInfoViewController: YMBaseViewController, ImagePickerDelegate {

    @IBOutlet weak var iconPickBtn: UIButton!
    
    @IBOutlet weak var nickField: UITextField!
    
    @IBOutlet weak var finishBtn: UIButton!
    
    var phone = ""
    var passwd = ""
    var verifyCode = ""
    
    enum VcType {
        case CompleteUserInfo
        case ChangeUserInfo
    }
    
    var type = VcType.CompleteUserInfo
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configUI()
        // Do any additional setup after loading the view.
    }
    
    func configUI() {
        if type == .CompleteUserInfo {
            navigationItem.title = "完善信息"
        }else{
            navigationItem.title = "修改信息"
        }
        
        finishBtn.clipsToBounds = true
        finishBtn.layer.cornerRadius = 5.0
        
        iconPickBtn.clipsToBounds = true
        iconPickBtn.layer.cornerRadius = iconPickBtn.width * 0.5
    }
    
    //选择头像
    @IBAction func pickPhoto(_ sender: UIButton) {
        let imagePickerVc = ImagePickerController()
        imagePickerVc.delegate = self
        imagePickerVc.imageLimit = 1
        present(imagePickerVc, animated: true, completion: nil)
    }
    
    @IBAction func finishClick(_ sender: UIButton) {
        if (nickField.text?.isEmpty)! {
            SVProgressHUD.showError(withStatus: "请填写昵称")
            return
        }
        
        sender.isUserInteractionEnabled = false
        SVProgressHUD.show(withStatus: "正在注册,请稍候...")
        
        UploadImageManager.upload((iconPickBtn.imageView?.image)!, success: {
            SDNetworkTool.shared.register(withPhone: self.phone, passwd: self.passwd, unionId: nil, smsCode: self.verifyCode, nickname: self.nickField.text!, headImage: (self.iconPickBtn.imageView?.image?.qiniuKey)!, finished: { (account:SDAccount) in
                //注册成功，直接登录
                SVProgressHUD.showSuccess(withStatus: "注册成功")
                //保存当前账号信息
                SDAccountTool.shared.saveAccount(account)
                //保存登陆状态，跳转登陆
                appDelegate.setLoginStatus(true)
            })
        }) {
            sender.isUserInteractionEnabled = true
            SVProgressHUD.showError(withStatus: "头像上传失败")
        }
    }
    
    //#Mark:UIImagePickerControllerDelegate
    func wrapperDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        
    }
    
    func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
        imagePicker.dismiss(animated: true, completion: nil)
        if let image = images.first {
            iconPickBtn.setImage(image, for: .normal)
        }
    }
    
    func cancelButtonDidPress(_ imagePicker: ImagePickerController) {
        imagePicker.dismiss(animated: true, completion: nil)
    }
}
